/**
 * Copyright (C), 2015-2024, XXX有限公司
 * FileName: Utils
 * Author:   mac
 * Date:     2024/3/29 18:01
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.finsiot.tools.taos.util;

import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author zhangtianchang
 * @create 2024/3/29
 * @since 1.0.0
 */
public class ParameterUtils {

    /**
     * 成员属性
     */
    public final static String DOUBLE_HORIZONTAL_BAR = "--";
    /**
     * 成员属性
     */
    public static Map<String, String> parse(String... args) {
        Map<String, String> parseMap = new HashMap(6);
        for (int i = 0; i < args.length; i++) {
            //检查是否是双横杠参数，非双横杠continue
            if (!args[i].contains(DOUBLE_HORIZONTAL_BAR)) {
                continue;
            }
            //获取双横杠参数对应数据
            String commandData = null;
            if (i + 1 < args.length && !args[i + 1].contains(DOUBLE_HORIZONTAL_BAR)) {
                commandData = args[i + 1];
            }
            //截取双横杠，获取参数名
            String parameter = args[i].substring(2);
            //存储参数、数据
            parseMap.put(parameter, commandData);
        }
        return parseMap;
    }

    public static String getHelp(Options options) {
        HelpFormatter helper = new HelpFormatter();

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        PrintWriter printWriter = new PrintWriter(byteArrayOutputStream);
        helper.printHelp(printWriter, HelpFormatter.DEFAULT_WIDTH, "java xx.jar -help", null,
                options, HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null);
        printWriter.flush();
        String help = byteArrayOutputStream.toString();
        printWriter.close();
        return help;
    }
}